using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using WIT.DAQ.Core.Helper;

namespace WIT.DAQ.Core.Forms;

/// <summary>
/// 性能监控窗口
/// </summary>
internal class PerformanceForm : BaseForm
{
	/// <summary>
	/// Required designer variable.
	/// </summary>
	private IContainer components = null;

	private Label label1;

	private Label label2;

	private TextBox txtMemory;

	private TextBox txtThreadCount;

	private Label label4;

	private TextBox txtMaxMemory;

	private Label label3;

	private Label label5;

	private TextBox txtMaxThreadCount;

	private Label label6;

	private Label lblMaxMemoryTime;

	private Label lblMaxThreadCountTime;

	/// <summary>
	/// 性能监控窗口
	/// </summary>
	internal PerformanceForm()
	{
		InitializeComponent();
	}

	private void PerformanceForm_Shown(object sender, EventArgs e)
	{
		Thread thread = new Thread((ThreadStart)delegate
		{
			try
			{
				while (true)
				{
					double currentMemory = Convert.ToDouble((Convert.ToDouble(WinformHelper.GetProcessMemory()) / 1024.0).ToString("0.0"));
					Invoke((Action)delegate
					{
						txtMemory.Text = currentMemory.ToString();
						if (!double.TryParse(txtMaxMemory.Text, out var result2) || currentMemory > result2)
						{
							txtMaxMemory.Text = currentMemory.ToString();
							lblMaxMemoryTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
						}
					});
					Thread.Sleep(1000);
				}
			}
			catch
			{
			}
		});
		thread.IsBackground = true;
		thread.Start();
		Thread thread2 = new Thread((ThreadStart)delegate
		{
			try
			{
				while (true)
				{
					int currentThreadCount = Process.GetCurrentProcess().Threads.Count;
					Invoke((Action)delegate
					{
						txtThreadCount.Text = currentThreadCount.ToString();
						if (!int.TryParse(txtMaxThreadCount.Text, out var result) || currentThreadCount > result)
						{
							txtMaxThreadCount.Text = currentThreadCount.ToString();
							lblMaxThreadCountTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
						}
					});
					Thread.Sleep(1000);
				}
			}
			catch
			{
			}
		});
		thread2.IsBackground = true;
		thread2.Start();
	}

	/// <summary>
	/// Clean up any resources being used.
	/// </summary>
	/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	/// <summary>
	/// Required method for Designer support - do not modify
	/// the contents of this method with the code editor.
	/// </summary>
	private void InitializeComponent()
	{
		this.label1 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.txtMemory = new System.Windows.Forms.TextBox();
		this.txtThreadCount = new System.Windows.Forms.TextBox();
		this.label4 = new System.Windows.Forms.Label();
		this.txtMaxMemory = new System.Windows.Forms.TextBox();
		this.label3 = new System.Windows.Forms.Label();
		this.label5 = new System.Windows.Forms.Label();
		this.txtMaxThreadCount = new System.Windows.Forms.TextBox();
		this.label6 = new System.Windows.Forms.Label();
		this.lblMaxMemoryTime = new System.Windows.Forms.Label();
		this.lblMaxThreadCountTime = new System.Windows.Forms.Label();
		base.SuspendLayout();
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(38, 39);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(89, 12);
		this.label1.TabIndex = 0;
		this.label1.Text = "当前占用内存：";
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(219, 39);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(17, 12);
		this.label2.TabIndex = 1;
		this.label2.Text = "MB";
		this.txtMemory.Enabled = false;
		this.txtMemory.Location = new System.Drawing.Point(133, 36);
		this.txtMemory.Name = "txtMemory";
		this.txtMemory.Size = new System.Drawing.Size(80, 21);
		this.txtMemory.TabIndex = 0;
		this.txtMemory.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.txtThreadCount.Enabled = false;
		this.txtThreadCount.Location = new System.Drawing.Point(133, 109);
		this.txtThreadCount.Name = "txtThreadCount";
		this.txtThreadCount.Size = new System.Drawing.Size(80, 21);
		this.txtThreadCount.TabIndex = 2;
		this.txtThreadCount.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.label4.AutoSize = true;
		this.label4.Location = new System.Drawing.Point(50, 112);
		this.label4.Name = "label4";
		this.label4.Size = new System.Drawing.Size(77, 12);
		this.label4.TabIndex = 3;
		this.label4.Text = "当前线程数：";
		this.txtMaxMemory.Enabled = false;
		this.txtMaxMemory.Location = new System.Drawing.Point(133, 63);
		this.txtMaxMemory.Name = "txtMaxMemory";
		this.txtMaxMemory.Size = new System.Drawing.Size(80, 21);
		this.txtMaxMemory.TabIndex = 1;
		this.txtMaxMemory.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.label3.AutoSize = true;
		this.label3.Location = new System.Drawing.Point(219, 66);
		this.label3.Name = "label3";
		this.label3.Size = new System.Drawing.Size(17, 12);
		this.label3.TabIndex = 6;
		this.label3.Text = "MB";
		this.label5.AutoSize = true;
		this.label5.Location = new System.Drawing.Point(38, 66);
		this.label5.Name = "label5";
		this.label5.Size = new System.Drawing.Size(89, 12);
		this.label5.TabIndex = 5;
		this.label5.Text = "最大占用内存：";
		this.txtMaxThreadCount.Enabled = false;
		this.txtMaxThreadCount.Location = new System.Drawing.Point(133, 136);
		this.txtMaxThreadCount.Name = "txtMaxThreadCount";
		this.txtMaxThreadCount.Size = new System.Drawing.Size(80, 21);
		this.txtMaxThreadCount.TabIndex = 3;
		this.txtMaxThreadCount.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
		this.label6.AutoSize = true;
		this.label6.Location = new System.Drawing.Point(50, 139);
		this.label6.Name = "label6";
		this.label6.Size = new System.Drawing.Size(77, 12);
		this.label6.TabIndex = 8;
		this.label6.Text = "最大线程数：";
		this.lblMaxMemoryTime.AutoSize = true;
		this.lblMaxMemoryTime.ForeColor = System.Drawing.Color.DarkGray;
		this.lblMaxMemoryTime.Location = new System.Drawing.Point(242, 66);
		this.lblMaxMemoryTime.Name = "lblMaxMemoryTime";
		this.lblMaxMemoryTime.Size = new System.Drawing.Size(0, 12);
		this.lblMaxMemoryTime.TabIndex = 9;
		this.lblMaxThreadCountTime.AutoSize = true;
		this.lblMaxThreadCountTime.ForeColor = System.Drawing.Color.DarkGray;
		this.lblMaxThreadCountTime.Location = new System.Drawing.Point(219, 139);
		this.lblMaxThreadCountTime.Name = "lblMaxThreadCountTime";
		this.lblMaxThreadCountTime.Size = new System.Drawing.Size(0, 12);
		this.lblMaxThreadCountTime.TabIndex = 10;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(384, 211);
		base.Controls.Add(this.lblMaxThreadCountTime);
		base.Controls.Add(this.lblMaxMemoryTime);
		base.Controls.Add(this.txtMaxThreadCount);
		base.Controls.Add(this.label6);
		base.Controls.Add(this.txtMaxMemory);
		base.Controls.Add(this.label3);
		base.Controls.Add(this.label5);
		base.Controls.Add(this.txtThreadCount);
		base.Controls.Add(this.label4);
		base.Controls.Add(this.txtMemory);
		base.Controls.Add(this.label2);
		base.Controls.Add(this.label1);
		base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
		base.MaximizeBox = false;
		base.Name = "PerformanceForm";
		this.Text = "性能监控";
		base.Shown += new System.EventHandler(PerformanceForm_Shown);
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
